import {addItem, addCounter} from './const'

export default {
    addCart(content, payload) {
        return new Promise((resolve, reject) => {
                // let item1 = null
            // for (let i = 0; i < state.cartList.length; i++) {
                
            //     if (state.cartList[i].iid === payload.iid) {
            //         item1 = state.cartList[i]
            //     }
            // }
        //    2. 方法二
            const item1 = content.state.cartList.find((item) => {
                return item.iid === payload.iid
            })
            
            
            // console.log(item1);

            if (item1) {
                // item1.count += 1
                resolve('商品加一')
                content.commit(addCounter, item1)
            } else {
                resolve('新的商品')
            payload.checked = true
            
                payload.count = 1
                // content.state.cartList.push(payload)
                content.commit(addItem, payload)
            }
        })
        
    }
}